local blaze = require "blaze"
local app = require "app"
local gamelib = require "club.threecard.gamelib"

local GameObjectExtension = CS.Blaze.GameObjectExtension

local log = app.logging.get("GameLobbyUI")

local M = gamelib.ui.activity()

M.store_watchers = {
    game = {
        ["当前游戏变更"] = function(self, store, state, args, init)
            self.game_title_txt.text = app.i18n.text(string.format("%s:title", state.package))
        end
    },
    rooms = {
        ["更新房间"] = function(self, store, state, args, init)
            if init and #state.map == 0 then
                return
            end
            
            local rooms = state.map
            self.room_list:fill(rooms)
        end
    }
}

function M:ctor()
    self.res = "club.threecard.casinolib/ui/gamelobby.b:GameLobbyUI"

    local Footer = require "club.threecard.casinolib.ui.gamelobby.Footer"
    local Header = require "club.threecard.casinolib.ui.gamelobby.Header"
    self.header = Header.new()
    self.footer = Footer.new()

    local SimpleList = require "blaze.ui.widget.SimpleList"
    local RoomItem = require "club.threecard.casinolib.ui.gamelobby.RoomItem"
    self.room_list = SimpleList.new()
    self.room_list.create_item = function(_, data, index)
        local ret = RoomItem.new(data)
        ret.res = string.format("club.threecard.casinolib/ui/gamelobby.b:style%s", index)
        return ret
    end

    local chip_anim = require "ui.Widget.chip_anim"
    self.online_bonus_anim = chip_anim.new()

    self.header.online_bonus.on_collected = function()
        local src = self.header.online_bonus:get_chip_transform()
        local dst = self.footer:get_chip_transform()
        self.online_bonus_anim:play(src, dst, 4)
        app.audio.post("online_bonus:collected")
    end

    self.more_games = require("club.threecard.gamelib.widgets.MoreGames").new()
end

function M:on_injected(bind)
    bind("activity.tweener", self.animator)
    bind("gameobject.element", self.header_obj, self.header)
    bind("gameobject.element", self.footer_obj, self.footer)
    bind("gameobject.element", self.more_games_obj, self.more_games)
    self.room_list:set_parent(self.room_list_root)
end

function M:on_release()
    self.room_list:release()
    self.header:release()
    self.footer:release()
end

return M